﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="ScrollableWidget.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

#endregion

    public abstract class ScrollableWidget : Widget, IUpdateableWidget
    {
        #region Private Members

#if WINDOWS_PHONE

        private const float flickSlowDown = 0.7f;
        private const float flickStopThresholdSq = 0.05f * 0.05f;

        private Vector2 flickSpeed;
        private Vector2 dragOffset;
        private Vector2 lastTouchLocation;

#endif
        private bool hscroll;
        private bool vscroll;
        private RectangleF scrollRegion;


        #endregion

        #region Constructors
        public ScrollableWidget(GuiManager manager, string name)
            : base(manager, name)
        {
            this.Init();
        }

        public ScrollableWidget(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
            this.Init();
        }
        #endregion

        ~ScrollableWidget()
        {
            Widgets.WidgetAdded -= this.Widgets_WidgetAdded;
            Widgets.WidgetRemoved -= this.Widgets_WidgetRemoved;
        }

        public bool HScroll
        {
            get
            {
                return this.hscroll;
            }

            set
            {
                this.hscroll = value;
            }
        }

        public bool VScroll
        {
            get
            {
                return this.vscroll;
            }

            set
            {
                this.vscroll = value;
            }
        }

        private void Init()
        {
            Widgets.WidgetAdded += new EventHandler(this.Widgets_WidgetAdded);
            Widgets.WidgetRemoved += new EventHandler(this.Widgets_WidgetRemoved);
        }

        private void Widgets_WidgetRemoved(object sender, EventArgs e)
        {
            Widget child = sender as Widget;
            if (child != null)
            {
                child.SizeChanged -= this.child_SizeChanged;
                child.LocationChanged -= this.child_LocationChanged;

                this.UpdateScrollRegion();
            }
        }

        private void Widgets_WidgetAdded(object sender, EventArgs e)
        {
            Widget child = sender as Widget;
            if (child != null)
            {
                child.SizeChanged += new EventHandler(this.child_SizeChanged);
                child.LocationChanged += new EventHandler(this.child_LocationChanged);

                this.UpdateScrollRegion();
            }
        }

        private void child_LocationChanged(object sender, EventArgs e)
        {
            this.UpdateScrollRegion();
        }

        private void child_SizeChanged(object sender, EventArgs e)
        {
            this.UpdateScrollRegion();
        }

        private void UpdateScrollRegion()
        {
            RectangleF scrollRegion = RectangleF.Empty;

            foreach (Widget w in this.Widgets)
            {
                scrollRegion = RectangleF.Union(w.ClientRectangle, scrollRegion);
            }

            this.scrollRegion = scrollRegion;

            this.hscroll = this.scrollRegion.Width > this.ClientRectangle.Width;
            this.vscroll = this.scrollRegion.Height > this.ClientRectangle.Height;
        }

        public void Update(GameTime gameTime)
        {
#if WINDOWS_PHONE
            
            TouchCollection touches = TouchPanel.GetState();

            if (touches.Count > 0)
            {
                if(this.ClickRectangle.Contains(touches[0].Position))
                {
                }
            }
            else
            {

            }

            while(TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();

               
            }
#endif
            
        }

#if WINDOWS_PHONE
        private void ProcessGesture(GestureSample gesture)
        {
             switch (gesture.GestureType)
                {
                    case GestureType.Flick:

                        break;

                    case GestureType.FreeDrag:
                        if (hscroll && vscroll)
                        {
                            
                        }
                        else
                        {
                            if (hscroll)
                            {
                                GestureSample hGesture = new GestureSample(GestureType.HorizontalDrag, gesture.Timestamp, gesture.Position, new Vector2(), gesture.Delta, new Vector2());
                                ProcessGesture(hGesture);
                            }
                            if (vscroll)
                            {
                                GestureSample vGesture = new GestureSample(GestureType.HorizontalDrag, gesture.Timestamp, gesture.Position, new Vector2(), gesture.Delta, new Vector2());
                                ProcessGesture(vGesture);
                            }
                        }
                        break;
                    case GestureType.HorizontalDrag:
                        if (hscroll)
                        {

                        }
                        break;
                    case GestureType.VerticalDrag:
                        if (vscroll)
                        {

                        }
                        break;
                    case GestureType.DragComplete:
                        break;
                }
        }

#endif

    }
}
